<?php

/**
 * lectura actions.
 *
 * @package    sf_sandbox
 * @subpackage lectura
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class lecturaActions extends sfActions {

    public function executeShow(sfWebRequest $request) {
        $this->lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->lectura);
    }

    public function executeIndex(sfWebRequest $request) {
        $this->lecturas = $this->getPager(LecturaTable::getBuscarLecturas("", ""), $request->getGetParameter('page'), "Lectura");
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new LecturaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new LecturaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id'))), sprintf('Object lectura does not exist (%s).', $request->getParameter('id')));
        $this->form = new LecturaForm($lectura);
        $this->codigo = $lectura->getUnidadFuncional()->getCodigo();
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id'))), sprintf('Object lectura does not exist (%s).', $request->getParameter('id')));
        $this->form = new LecturaForm($lectura);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id'))), sprintf('Object lectura does not exist (%s).', $request->getParameter('id')));
        $lectura->delete();

        $this->redirect('lectura/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $lectura = $form->save();
            $this->redirect('lectura/index');
        }
    }

    public function executeBuscar(sfWebRequest $request) {
        $this->setLayout("busqueda");
    }

    public function executeBuscarLectura(sfWebRequest $request) {
        $this->setLayout("busqueda");
    }

    protected function getPager($q, $page, $model) {
        $pager = new sfDoctrinePager($model, sfConfig::get('app_nro_x_pagina_contribuyente'));
        $pager->setQuery($q);
        $pager->setPage($page);
        $pager->init();

        return $pager;
    }

    public function executeFiltrarUnidadesFuncionales(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $dato = $request->getPostParameter("dato");
        } else {
            $dato = $request->getGetParameter("dato");
        }

        $this->unidadesFuncionales = $this->getPager(UnidadFuncionalTable::getBuscarUnidadesPopUp($dato), $request->getGetParameter('page'), "UnidadFuncional");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("lectura/resultadoUnidadesFuncionales", array('unidadesFuncionales' => $this->unidadesFuncionales));
        }
    }

    public function executeBuscarlecturas(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $nombre = $request->getPostParameter("nombre");
            $apellido = $request->getPostParameter("apellido");
        } else {
            $nombre = $request->getGetParameter("nombre");
            $apellido = $request->getGetParameter("apellido");
        }
        $this->lecturas = $this->getPager(LecturaTable::getBuscarLecturas($nombre, $apellido), $request->getGetParameter('page'), "Lectura");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("lectura/listadoLecturas", array('lecturas' => $this->lecturas));
        }
    }

    public function executeEliminar(sfWebRequest $request) {
           $this->forward404Unless($this->lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id'))), sprintf('Object lectura does not exist (%s).', $request->getParameter('id')));
    
           
    }

//    public function executeVer(sfWebRequest $request) {
//        $this->forward404Unless($lectura = Doctrine_Core::getTable('Lectura')->find(array($request->getParameter('id'))), sprintf('Object lectura does not exist (%s).', $request->getParameter('id')));
//        $this->form = new LecturaFormForm($lectura);
//        //$this->form->setTipoForm('ver');
//    }

    public function executeBuscarultimalectura(sfWebRequest $request) {
        $arraySugerencias = Array();
        $idUF = $_REQUEST['id'];
        $lectura = LecturaTable::getUltimaLectura($idUF);
        foreach ($lectura as $i => $l) {
            $arrayLectura[$i]['fecha'] = date('d-m-Y', strtotime($l->fecha));
            $arrayLectura[$i]['valor'] = $l->valor;
        }
        return $this->renderText(json_encode($arrayLectura));
    }
    public function executeImprimirplanilla(sfWebRequest $request){
        $unidadesfuncionales = UnidadFuncionalTable::getTodas();
        $html = $this->getPartial('lectura/imprimirplanilla', array('unidadesfuncionales' => $unidadesfuncionales));
       
        $config = sfTCPDFPluginConfigHandler::loadConfig();

          // pdf object
          $pdf = new sfTCPDF();

          // set document information
          $pdf->SetCreator(PDF_CREATOR);
          $pdf->SetAuthor('NORDSOFT');
          //$pdf->SetTitle('Planilla Relevamiento de Lecturas');
         // set default header data
          $pdf->SetHeaderData("","", PDF_HEADER_TITLE, PDF_HEADER_STRING);

          // set header and footer fonts
          $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
          $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

          // set default monospaced font
          $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

          //set margins
          $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
          $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
          $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

          //set auto page breaks
          $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

          //set image scale factor
          $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

          // ---------------------------------------------------------

          // set default font subsetting mode
          $pdf->setFontSubsetting(true);

          // Set font
          // dejavusans is a UTF-8 Unicode font, if you only need to
          // print standard ASCII chars, you can use core fonts like
          // helvetica or times to reduce file size.
          $pdf->SetFont('dejavusans', '', 14, '', true);

          // Add a page
          // This method has several options, check the source code documentation for more information.
          $pdf->AddPage();

          // Set some content to print
         

          // Print text using writeHTMLCell()
          $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

          // ---------------------------------------------------------

          // Close and output PDF document
          // This method has several options, check the source code documentation for more information.
          $pdf->Output('example_001.pdf', 'I');

          // Stop symfony process
          throw new sfStopException();
        
    }

}
